﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FinPlusCommon;
using System.ComponentModel;
using System.Text.RegularExpressions;
using u = FinPlusUtility.Utility;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusValueChangeTextBox : FinPlusControl
    {
        public override string Id { get { return GetValue(IdProperty).ToString(); } set { } }
        public override string[] States { get { return new string[] { }; } }
        public override string[] TestStates { get { return new string[] { "ModelTest", "ViewTest", "IsValid" }; } }
        public override IFinPlusFunctions Model { get { return null; } }
        public override string ModelTest { get { return ModelToString(); } }
        public override bool IsValid { get; set; }
 
        public string ViewTest { get { return ToString(); } }

        public string FunctionName { get { return GetValue(FunctionNameProperty).ToString(); } set { } }
        public string Text { get { return GetValue(TextProperty).ToString(); } set { SetValue(TextProperty, value); } }
        public int Status { get { return (int)GetValue(StatusProperty); } set { SetValue(StatusProperty, value); } }
        public string StringFormat { get { return GetValue(StringFormatProperty).ToString(); } }

        public static readonly DependencyProperty IdProperty = DependencyProperty.Register(Propertys.Id.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox));
        public static readonly DependencyProperty FunctionNameProperty = DependencyProperty.Register(Propertys.FunctionName.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox));
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(Propertys.Text.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox), new UIPropertyMetadata(Callback));
        public static readonly DependencyProperty StatusProperty = DependencyProperty.Register(Propertys.Status.ToString(), typeof(int), typeof(FinPlusValueChangeTextBox));
        public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register(Propertys.StringFormat.ToString(), typeof(string), typeof(FinPlusValueChangeTextBox));
      
        private enum Propertys { Id, FunctionName, Text, IsEnabled, Status, StringFormat, InputMask }
        
        private double _oldValue = double.MinValue;

        //construct
        public FinPlusValueChangeTextBox()
        {
            InitializeComponent();
            IsValid = true;

            textBlock.Cursor = ((TextBlock)this.Resources["CPoint"]).Cursor;
        }

        //common control interface
        public override void Dock(object control, string dock) 
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public override void Initialise()
        {

        }

        public override void CommandRouter(IFinPlusFunction func) {  }

        public override void Dispose() { }

        public new string ToString()
        {
            return string.Format("{0}|{1}", textBlock.IsEnabled, textBlock.Text);
        }

        public string ModelToString()
        {
            return string.Format("{0}|{1}", IsEnabled, Text);
        }

        //private
        private void SetControl(DependencyPropertyChangedEventArgs a)
        {
            //format
            double newValue;
            if (double.TryParse(a.NewValue.ToString(), out newValue))
            {
                textBlock.Foreground = newValue < 0 ? Brushes.Red : Brushes.Black;
                Status = _oldValue == double.MinValue ? 0 : newValue > _oldValue ? 1 : -1;
                _oldValue = newValue;
            }
            else
                Status = 0;
        }

        //event
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueChangeTextBox)d).SetControl(a); }
    }   
}
